<?php
//WP Cart Product Management Menu
function wp_dk_estore_product_management_menu_tpl()
{

	echo '<div class="wrap">
	<h2>'.__('Manage Products', 'wp_eStore').'</h2>';
	echo '<div id="poststuff"><div id="post-body">';
	   	
	$eStore_products_per_page = get_option('eStore_products_per_page');
	if (empty($eStore_products_per_page)) 
	{
        echo '<div id="message" class="updated fade"><p>';
        echo 'It appears that you have never saved your settings after installing the plugin! Please visit the settings page of this plugin and save it.';
        echo '</p></div>';  			
	}
	?>
	<br />
	<div class="postbox">
	<h3><label for="title">Product Search</label></h3>
	<div class="inside">
	<br /><strong>Search for a product by entering the full or partial product Name</strong>
	<br /><br />
	<form method="post" action="<?php echo $_SERVER["REQUEST_URI"]; ?>">
    <input type="hidden" name="info_update" id="info_update" value="true" />
    
    <input name="eStore_product_search" type="text" size="30" value=""/>
    <div class="submit">
        <input type="submit" name="info_update" value="Search &raquo;" />
    </div>   
    </form>
    </div></div>
	<?php

	if (isset($_POST['limit_update']))
	{
		update_option('eStore_manage_products_limit2', (string)$_POST["eStore_manage_products_limit2"]);
	}
    $limit = get_option('eStore_manage_products_limit2');
    if(empty($limit))
    {
        update_option('eStore_manage_products_limit2', 50);
        $limit = 50;
    }
    
	//hard code limit 
	$limit = 1000;
	
    if(isset($_POST['Delete']))
    {
        if(wp_eStore_delete_product_data($_POST['prod_id']))
        {
			//delete products belong to user
			dk_eshop_delete_product_belong_user($_POST['prod_id']);
            $message = "Product successfully deleted";
        }
        else
        {
            $message = "An error occurded while trying to delete the entry";
        }
        echo '<div id="message" class="updated fade"><p><strong>';
	    echo $message;
	    echo '</strong></p></div>';
    }
    	       
	if (isset($_POST['info_update']))
	{
		$search_term = (string)$_POST["eStore_product_search"];
		update_option('eStore_product_search', (string)$_POST["eStore_product_search"]);
		eStore_dk_display_searched_products($search_term);
	}
	else
	{
		eStore_dk_display_products($limit);
	}

	?>
    <?php
    echo '</div></div>';
	echo '</div>';
}


function eStore_dk_display_searched_products($search_term)
{
	echo '
	<table class="widefat">
	<thead><tr>
	<th scope="col">'.__('ID', 'wp_eStore').'</th>
	<th scope="col">'.__('Image', 'wp_eStore').'</th>
	<th scope="col">'.__('Product Name', 'wp_eStore').'</th>
	<th scope="col">'.__('Price', 'wp_eStore').'</th>
	<th scope="col">'.__('Download Link', 'wp_eStore').'</th>
	<th scope="col">'.__('Sales Count', 'wp_eStore').'</th>
	<th scope="col">'.__('Available Copies', 'wp_eStore').'</th>
	<th scope="col"></th>
	</tr></thead>
	<tbody>';

	global $wpdb;
	global $products_table_name;

    $wp_eStore_db = $wpdb->get_results("SELECT * FROM $products_table_name WHERE name like '%".$search_term."%'", OBJECT);
	if ($wp_eStore_db)
	{
		foreach ($wp_eStore_db as $wp_eStore_db)
		{
			//check if record is belong to current user
			$mk_current_user_id = get_option('dk_eshop_user_product_record'. $wp_eStore_db->id);
			if(dk_eshop_get_e_member_id() != $mk_current_user_id )
				continue;
			
			echo '<tr>';
			echo '<td>'.$wp_eStore_db->id.'</td>';
			if(!empty($wp_eStore_db->thumbnail_url))
			{
				echo '<td><img src="'.$wp_eStore_db->thumbnail_url.'" width="50" height="50"></td>';
			}
			else
			{
				echo '<td><img src="'.WP_ESTORE_URL.'/images/no-image-specified.gif" width="50" height="50"></td>';
			}	
			echo '<td><strong>'.$wp_eStore_db->name.'</strong></td>';
			echo '<td><strong>'.$wp_eStore_db->price.'</strong></td>';
			echo '<td><strong>'.$wp_eStore_db->product_download_url.'</strong></td>';
			echo '<td><strong>'.$wp_eStore_db->sales_count.'</strong></td>';			
			$available_copies = $wp_eStore_db->available_copies;							
			if(empty($available_copies))
			{
				if($available_copies == ''){
					$available_copies = '&#8734;';					
				}
				else{
					$available_copies = '0';
				}
			}
			echo '<td><strong>'.$available_copies.'</strong></td>';	
			
			echo '<td style="text-align: center;"><a href="admin.php?page=wp_eStore_addedit&editproduct='.$wp_eStore_db->id.'">'.__('Edit', 'wp_eStore').'</a>';
			
			echo "<form method=\"post\" action=\"\" onSubmit=\"return confirm('Are you sure you want to delete this entry?');\">";				
			echo "<input type=\"hidden\" name=\"prod_id\" value=".$wp_eStore_db->id." />";
            echo '<input style="border: none; background-color: transparent; padding: 0; cursor:pointer;" type="submit" name="Delete" value="Delete">';
            echo "</form>";
           	echo "</td>";
			
           	
			echo '</tr>';
		}
	}
	else
	{
		echo '<tr> <td colspan="8">'.__('No Product found.', 'wp_eStore').'</td> </tr>';
	}

	echo '</tbody>
	</table>';
    
	// Add product button
	echo '<br /><br /><a href="admin.php?page=wp_eStore_addedit" class="button rbutton">'.__('Add New Product', 'wp_eStore').'</a>';
}


function eStore_dk_display_products($limit)
{
	echo '
	<table class="widefat">
	<thead><tr>
	<th scope="col">'.__('ID', 'wp_eStore').'</th>
	<th scope="col">'.__('Image', 'wp_eStore').'</th>';
	
	echo '<th scope="col">';	
	if(isset($_GET['order']) && $_GET['order'] == 'asc'){
		echo 'Product Name <a href="admin.php?page=wp-cart-for-digital-products/wp_eStore1.php&orderby=name&order=desc"><img src="'.WP_ESTORE_IMAGE_URL.'/sort-desc-icon.gif" title="Sort by product name"></a>';
	}	
	else{
		echo 'Product Name <a href="admin.php?page=wp-cart-for-digital-products/wp_eStore1.php&orderby=name&order=asc"><img src="'.WP_ESTORE_IMAGE_URL.'/sort-asc-icon.gif" title="Sort by product name"></a>';		
	}	
	echo '</th>';
	
	echo '<th scope="col">'.__('Price', 'wp_eStore').'</th>
	<th scope="col">'.__('Download Link', 'wp_eStore').'</th>
	<th scope="col">'.__('Sales Count', 'wp_eStore').'</th>
	<th scope="col">'.__('Available Copies', 'wp_eStore').'</th>
	<th scope="col"></th>
	</tr></thead>
	<tbody>';

    if (isset($_GET['product_page']))
    {
        $page = $_GET['product_page'];
    }
    else
    {
        $page = 1;
    }
    $start = ($page - 1) * $limit;

	global $wpdb;
	global $products_table_name;

	if(isset($_GET['order']))
	{		
		$order_by = $_GET['orderby'];
		$order = $_GET['order'];
		$wp_eStore_db = $wpdb->get_results("SELECT * FROM $products_table_name ORDER BY $order_by $order LIMIT $start, $limit", OBJECT);
	}
	else{
		$wp_eStore_db = $wpdb->get_results("SELECT * FROM $products_table_name ORDER BY id DESC LIMIT $start, $limit", OBJECT);	
	}
		
	//$wp_eStore_db = $wpdb->get_results("SELECT * FROM $products_table_name ORDER BY id DESC LIMIT $start, $limit", OBJECT);
    //get total rows
    $totalrows = $wpdb->get_var("SELECT COUNT(*) FROM $products_table_name;");

	$i = 0;
	if ($wp_eStore_db)
	{
		foreach ($wp_eStore_db as $wp_eStore_db)
		{
			//check if record is belong to current user
			$mk_current_user_id = get_option('dk_eshop_user_product_record'. $wp_eStore_db->id);
			if(dk_eshop_get_e_member_id() != $mk_current_user_id )
				continue;
		
			if($i%2 == 0)
            {
				echo "<tr style='background-color: #fff;'>";
				$i++;
			}
			else
			{
				echo "<tr style='background-color: #E9EDF5;'>";
				$i++;
			}			
			//echo '<tr>';
			echo '<td>'.$wp_eStore_db->id.'</td>';			
			if(!empty($wp_eStore_db->thumbnail_url))
			{
				echo '<td><img src="'.$wp_eStore_db->thumbnail_url.'" width="50" height="50"></td>';
			}
			else
			{
				echo '<td><img src="'.WP_ESTORE_URL.'/images/no-image-specified.gif" width="50" height="50"></td>';
			}				
			echo '<td><strong>'.$wp_eStore_db->name.'</strong></td>';
			echo '<td><strong>'.$wp_eStore_db->price.'</strong></td>';
			echo '<td><strong>'.$wp_eStore_db->product_download_url.'</strong></td>';
			echo '<td><strong>'.$wp_eStore_db->sales_count.'</strong></td>';
			$available_copies = $wp_eStore_db->available_copies;
			if(empty($available_copies))
			{
				if($available_copies == ''){
					$available_copies = '&#8734;';					
				}
				else{
					$available_copies = '0';
				}
			}
			echo '<td><strong>'.$available_copies.'</strong></td>';			
		
           
			echo '<td style="text-align: center;"><a href="admin.php?page=dk_eStore_addedit&editproduct='.$wp_eStore_db->id.'">'.__('Edit', 'wp_eStore').'</a>';
			
			
			echo "<form method=\"post\" action=\"\" onSubmit=\"return confirm('Are you sure you want to delete this entry?');\">";				
			echo "<input type=\"hidden\" name=\"prod_id\" value=".$wp_eStore_db->id." />";
            echo '<input style="border: none; background-color: transparent; padding: 0; cursor:pointer;" type="submit" name="Delete" value="Delete">';
            echo "</form>";
           	echo "</td>";
			
			echo '</tr>';
		}
	}
	else
	{
		echo '<tr> <td colspan="8">'.__('No Products found.', 'wp_eStore').'</td> </tr>';
	}

	echo '</tbody>
	</table>';
    
    //Number of pages setup
	$pages = ceil($totalrows / $limit)+1;
    for($r = 1;$r<$pages;$r++) 
    {
        echo "<a href='?page=dk_eshop/dk_eshop.php&product_page=".$r."' class=\"button rbutton\">".$r."</a>&nbsp;";
        if ($r%15==0)
           echo '<br /><br />';        
    }

	// Add product button
	echo '<br /><br /><a href="admin.php?page=dk_eStore_addedit" class="button rbutton">'.__('Add New Product', 'wp_eStore').'</a>';
}

?>